<?php

include('setup.php');
$setup = new setup();
$detail="";
$hostname_logon = 'localhost';      //Database server LOCATION
    $database_logon = 'wwwmsc_newcareer';       //Database NAME
    $username_logon = 'wwwmsc';       //Database USERNAME
    $password_logon = 'Slpnxhe}bX4h';       //Database PASSWORD
	$database_connection = mysql_connect($hostname_logon,$username_logon,$password_logon) or die("couldnt connect with mysql");
	$database_selection = mysql_select_db($database_logon,$database_connection);
	mysql_query( "SET NAMES utf8", $database_connection );
        mysql_query( "SET CHARACTER SET utf8", $database_connection );
//echo $_SESSION["userid"];

 if (isset($_SESSION['userid']))
  {
    
     $userid = $_SESSION["userid"];

     $displayList = array();
     
	$str1 = "SELECT * FROM USERS WHERE user_id = '$userid'";

     $result1=mysql_query($str1) or die(mysql_error());

     $total=mysql_num_rows($result1);

    $line=mysql_fetch_array($result1);
	$user_detail=="";
	if($line['user_level']==4)
	{
    $user_detail="select * from USERS_DETAILS where userd_id=".$_SESSION['userid'];
	}
	if($line['user_level']==3)
	{
	 $user_detail="select * from COMPANIES_DETAILS where comp_id=".$_SESSION['userid'];
	}
	if($user_detail!="")
	{
	$detail_result=mysql_query($user_detail);
	$detail=mysql_fetch_array($detail_result);
	}
  //$name = $line["user_fname"]." ".$line["user_lname"];

  //$_SESSION["password"]= $line["password"];
  
 
 	$select_location_query = "select * from LOCATION where loc_id = '".$line['user_locid']."'";
	$select_location_result = mysql_query($select_location_query) or die(mysql_error());
	$select_location_data = mysql_fetch_array($select_location_result);


  }
 else
  {
       echo "please login";
  }
?>


<?php
// αν έχει γίνει η καταχώρηση εμφανίζεται το αντίστοιχο μήνυμα, αλλιώς εμφανίζεται η φόρμα καταχώρησης
if ($_GET['EditRegistration'] == "success") 
{
	echo 'Your registration was succesfully updated';
} 
else if($_GET['EditRregistration'] == "failed") 
{
	echo 'Error: Your registration was not succesfully updated';
}
else
{
?>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script language="javascript">
	function check_validation()
	{
		// validation for username
		if(document.getElementById("user_name").value.split(" ").join("") == "")
		{
			alert("Please enter username.");
			document.getElementById("user_name").focus();
			//jQuery("#user_name_error_value").html("Please enter username.");
			//jQuery("#user_name_error").fadeIn("slow");
			return false;
		}
		else
		{
			//jQuery("#user_name_error").fadeOut("slow");
		}
		
		// validation for users password
		if ($("#user_passwd").val() == "") {
			alert("Please enter a password.");
			$("#user_passwd").focus();
			jQuery("#user_passwd_error_value").html("Please enter a password.");
			//jQuery("#user_passwd_error").fadeIn("slow");
			return false;
		} else if (($("#user_passwd").val().length < 7) || ($("#user_passwd").val().length > 15)) {
			alert("You password should have 7 to 15 characters.");
			$("#user_passwd").focus();
			jQuery("#user_passwd_error_value").html("You password should have 7 to 15 characters.");
			//jQuery("#user_passwd_error").fadeIn("slow");
			return false;
		}
		else
		{
			//jQuery("#user_passwd_error").fadeOut("slow");
		}
		
		// validation for users first name
		if(document.getElementById("user_fname").value.split(" ").join("") == "")
		{
			alert("Please enter your first name.");
			document.getElementById("user_fname").focus();
			jQuery("#user_fname_error_value").html("Please enter your first name.");
			//jQuery("#user_fname_error").fadeIn("slow");
			return false;
		}
		
		// validation for users last name
		if(document.getElementById("user_lname").value.split(" ").join("") == "")
		{
			alert("Please enter your surname.");
			document.getElementById("user_lname").focus();
			jQuery("#user_lname_error_value").html("Please enter your surname.");
			//jQuery("#user_lname_error").fadeIn("slow");
			return false;
		}
		
		//http://www.w3resource.com/javascript/form/phone-no-validation.php
		// validation for users primary phone number
		if(document.getElementById("user_phone1").value.split(" ").join("") == "")
		{
			alert("Please enter your primary phone number.");
			document.getElementById("user_phone1").focus();
			jQuery("#user_phone1_error_value").html("Please enter your primary phone number.");
			//jQuery("#user_phone1_error").fadeIn("slow");
			return false;
		} else if (($("#user_phone1").val().length < 8)) {
			alert("You phone number should have minimum 8 characters.");
			$("#user_phone1").focus();
			jQuery("#user_phone1_error_value").html("You phone number should have minimum 8 characters.");
			//jQuery("#user_phone1_error").fadeIn("slow");
			return false;
		}

		// validation for users secondary phone number
		if(document.getElementById("user_phone2").value.split(" ").join("") == "")
		{
			alert("Please enter your mobile number.");
			document.getElementById("user_phone2").focus();
			jQuery("#user_phone2_error_value").html("Please enter your mobile number.");
			//jQuery("#user_phone2_error").fadeIn("slow");
			return false;
		}
		
		// validation for users location
		if(document.getElementById("autocomplete").value.split(" ").join("") == "")
		{
			alert("Please enter your location.");
			document.getElementById("autocomplete").focus();
			jQuery("#autocomplete_error_value").html("Please enter your location.");
			//jQuery("#autocomplete_error").fadeIn("slow");
			return false;
		}
		if(document.getElementById("user_email").value.split(" ").join("") == "")
			{
				alert("Please enter your email address.");
				//alert('Please enter your email address.'); 
				document.getElementById("user_email").focus();
				jQuery("#user_email_error_value").html("Please enter your location.");
				//jQuery("#user_email_error").fadeIn("slow");
				return false;
			}
			else
			{
				var emailPat=/^(.+)@(.+)$/
				var matchArray=document.getElementById("user_email").value.match(emailPat)
			
				if (matchArray==null) 
				{
					//alert("")
					alert('The email address that you have entered is invalid.'); 
					document.getElementById("user_email").focus();
					jQuery("#user_email_error_value").html("The email address that you have entered is invalid.");
					//jQuery("#user_email_error").fadeIn("slow");
					return false;
				}
		}
		if(document.getElementById("user_website").value.split(" ").join("") == "")
		{
			alert("Please enter your website url.");
			document.getElementById("user_website").focus();
			jQuery("#user_website_error_value").html("The email address that you have entered is invalid.");
			//jQuery("#user_website_error").fadeIn("slow");
			return false;
		}
		return true;
	}
	function check_validation_of_field($value_of_field,$type_of_field,user_id)
	{
		if($value_of_field != "")
		{
			$.post( "http://192.185.116.17/~wwwmsc/includes/check_validation.php?field_type="+$type_of_field+"&field_value="+$value_of_field+"&user_id="+user_id, function( data ) {
				if($type_of_field == "user_name")
				{
					if(data == "no_conflict")
					{
						jQuery("#user_name_error").fadeOut("slow");
					}
					else
					{
						jQuery("#user_name_error_value").html("The username that you have entered already exists, Please enter different username.");
						jQuery("#user_name_error").fadeIn("slow");
					}
				}
				else if($type_of_field == "user_email")
				{
					if(data == "no_conflict")
					{
						jQuery("#user_email_error").fadeOut("slow");
					}
					else
					{
						jQuery("#user_email_error_value").html("The email that you have entered already exists, Please enter different email.");
						jQuery("#user_email_error").fadeIn("slow");
					}
				}
			});
		}
	}
</script>
<div id="main">
    <div id="editUser">
    Στοιχεία Χρήστη
    <form name="EditRegistrationForm" action="includes/EditUserRegister.php" method="POST" onsubmit="javascript:return check_validation();" enctype="multipart/form-data">
	<table width="100%" border="0" align="center">             
    	<tr>
        	<td width="100%" align="right">Username:</td>
            <td width="100%" align="left"><input type="text" id="user_name" name="user_name"  size="30" value="<?= $line['user_name']?>" onblur="check_validation_of_field(this.value,'user_name','<?php echo $_SESSION["userid"]; ?>')"></td>
        </tr>
        <tr id="user_name_error" style="display:none;">
        	<td width="100%" align="right">&nbsp;</td>
        	<td width="100%" align="left" style="color:#FF0000;" id="user_name_error_value"></td>
        </tr>
    	<tr>
        	<td width="100%" align="right">Όνομα:</td>
            <td width="100%" align="left"><input type="text" id="user_fname" name="user_fname"  size="30" value="<?= $line['user_fname']?>"></td>
        </tr>
    	<tr>
        	<td width="100%" align="right">Επώνυμο:</td>
            <td width="100%" align="left"><input type="text" id="user_lname" name="user_lname"  size="30" value="<?= $line['user_lname']?>"></td>
        </tr>
    	<tr>
        	<td width="100%" align="right">Τηλέφωνο:</td>
            <td width="100%" align="left"><input type="text" id="user_phone1" name="user_phone1"  size="30" value="<?= $line['user_phone1']?>"></td>
        </tr>
    	<tr>
        	<td width="100%" align="right">Κινητό:</td>
            <td width="100%" align="left"><input type="text" id="user_phone2" name="user_phone2"  size="30" value="<?= $line['user_phone2']?>"></td>
        </tr>
        <tr>
        	<td width="100%" align="right">Διεύθυνση:</td>
            <td width="100%" align="left">
                <div id="locationField">
                  <input id="autocomplete" placeholder="Εισάγετε τη διεύθυνση σας για αλλαγή της υπάρχουσας"
                         onFocus="geolocate()" type="text" name="autocomplete" value="<?= $line['user_autocomplete']?>" style="width:44%;"></input>
                         <input type="hidden" name="old_address"  value="<?= $line['user_autocomplete']?>" />
                </div>
            
                <table id="address" hidden="false">
                  <tr>
                    <td class="label">Διεύθυνση</td>
                    <td class="slimField"><input class="field" id="street_number" name="street_number" disabled="true" value="<?= $select_location_data['loc_street_number']?>"></input></td>
                    <td class="wideField" colspan="2"><input class="field" id="route" name="route" disabled="true" value="<?= $select_location_data['loc_route']?>"></input></td>
                  </tr>
                  <tr>
                    <td class="label">Πόλη</td>
                    <td class="wideField" colspan="3"><input class="field" id="locality" name="locality" disabled="true" value="<?= $select_location_data['loc_locality']?>"></input></td>
                  </tr>
                  <tr>
                    <td class="label">Νομός</td>
                    <td class="slimField"><input class="field" id="administrative_area_level_1" name="administrative_area_level_1" disabled="true" value="<?= $select_location_data['loc_administrative_area_level_1']?>"></input></td>
                    <td class="label">Τ.Κ.</td>
                    <td class="wideField"><input class="field" id="postal_code" name="postal_code" disabled="true" value="<?= $select_location_data['loc_postal_code']?>"></input></td>
                  </tr>
                  <tr>
                    <td class="label">Χώρα</td>
                    <td class="wideField" colspan="3"><input class="field" id="country" disabled="true" value="<?= $select_location_data['loc_country']?>"></input></td>
                  </tr>
                </table>
            </td>
        </tr>
    	<tr>
        	<td width="100%" align="right">Email:</td>
            <td width="100%" align="left"><input type="text" id="user_email" name="user_email"  size="30" value="<?= $line['user_email']?>" onblur="check_validation_of_field(this.value,'user_email','<?php echo $_SESSION["userid"]; ?>')"></td>
        </tr> 
        <tr id="user_email_error" style="display:none;">
        	<td width="100%" align="right">&nbsp;</td>
        	<td width="100%" align="left" style="color:#FF0000;" id="user_email_error_value"></td>
        </tr>
    	<tr>
        	<td width="100%" align="right">Ιστότοπος:</td>
            <td width="100%" align="left">
            	<input type="text" id="user_website" name="user_website" size="30" value="<?= $line['user_website']?>">
                <input type="hidden" name="userid" id="userid" value="<?php echo $_SESSION["userid"]; ?>" />
               
            </td>
        </tr> 
        <!-- Start Individual USER DETAIL -->
        <?php if($line['user_level']=="4"){?>
                 <tr>
                            <td class="label">ηλικία</td>
                            <td class="wideField" colspan="3"><input class="field" id="userd_age" name="userd_age" style="width:44%;"  value="<?= $detail['userd_age']?>"></input></td>
                          </tr>
                 <tr>
                            <td class="label">κατηγορία</td>
                            <td class="wideField" colspan="3">
                           <select name="user_cat"  style="width:44%;">
					<option value="0" >Επιλέξτε κατηγορία</option>	<?php //Emfanisi ton katigorion apo tin vasi gia provoli se drop down menu ?>
						<?php 
						$result = $setup->qry("Select * from CATEGORIES");
						while($row = mysql_fetch_assoc($result)) {	 ?>
							<option value="<?php echo $row['cat_id']; ?>" <?php if($detail['userd_catid']==$row['cat_id']){?> selected="selected"<?php }?>> <?php echo $row['cat_desc']; ?> </option>
					<?php } ?>
				</select>	
                           
                            </td>
                          </tr>
                 <tr>
                            <td class="label">εκπαίδευση</td>
                            <td class="wideField" colspan="3"><input class="field" size="30" id="userd_education" style="width:44%;" name="userd_education"  value="<?= $detail['userd_education']?>"></input></td>
                          </tr>
                 <tr>
                            <td class="label">CV</td>
                            <td class="wideField" colspan="3">
                            <input type="hidden" name="old_file" value="<?php echo $detail['userd_cv'];?>" />
                            <input class="field" type="file" id="cv" name="cv"  value=""></input><br />
                            <?php $site=$_SERVER['HTTP_HOST'];?>
                            <?php if($detail['userd_cv']!="" && file_exists("cv/".$detail['userd_cv'])){?><a href="http://192.185.116.17/~wwwmsc/cv/<?php echo $detail['userd_cv'];?>" target="_blank"><img src="images/pdf_icon.png" width="60"></a><? }?>
                            </td>
                          </tr>
                          
          <? }?>        
                  <!-- End Company USER DETAIL -->
                  
                   <!-- Start Company USER DETAIL -->
        <?php if($line['user_level']=="3"){?>
                 <tr>
                            <td class="label">Όνομα εταιρείας</td>
                            <td class="wideField" colspan="3"><input class="field" id="comp_name" name="comp_name" size="30" style="width:44%;"  value="<?= $detail['comp_name']?>"></input></td>
                          </tr>
                 <tr>
                            <td class="label">Λογότυπο Εταιρείας</td>
                            <td class="wideField" colspan="3">
                            <input type="hidden" name="logo_old_file" value="<?php echo $detail['comp_logo'];?>" />
                            <input class="field" type="file" id="comp_logo" name="comp_logo"  value=""></input><br />
                            <?php if($detail['comp_logo']!="" && file_exists("logo/".$detail['comp_logo'])){?><img src="logo/<?php echo $detail['comp_logo'];?>" /><? }?>
                            </td>
                          </tr>
                 <tr>
                            <td class="label">Περιγραφή Εταιρείας</td>
                            <td class="wideField" colspan="3"><input class="field" style="width:44%;" id="comp_desc" name="comp_desc"  value="<?= $detail['comp_desc']?>"></input></td>
                          </tr>
                          
                 
                          
          <? }?>        
                  <!-- End Individual USER DETAIL -->
                  
    	<tr>
        	<td width="100%" align="right">&nbsp;</td>
            <td width="100%" align="left"><input type="Submit"  value="Ανανέωση Στοιχείων"></td>
        </tr> 
    </table>
</form>
<br><br>

<? } ?>

	</div>
    
    <div id="favorites">
    Αγαπημένα
    <table>
    <?php $start=0;$end=5;
	if($_REQUEST['page'] && $_REQUEST['page'] > 1)
	{
	$start=5*$_REQUEST['page']-5;
	
	}
	
	
	$limit="LIMIT ".$start.",5";
	
	  $sel_fav = "select * from FAVOURITES where  fav_userid='".$_SESSION['userid']."' ".$limit;
	
	$fav_result=mysql_query($sel_fav)or die(mysql_error());
	$fav_rows=mysql_num_rows($fav_result);
	$count=0;
	if($fav_rows > 0)
	{
			while($fav_row=mysql_fetch_array($fav_result)){
			$ad_detail=get_ad_data($fav_row['fav_adid']);
			$count++;
			
			?><tr><td width="150" align="left"><a href="http://192.185.116.17/~wwwmsc/portal.php?action=search&advno=<?php echo $ad_detail['ad_id'];?>"><?php echo $ad_detail['ad_title'];?></a></td></tr><?php
			
			}
	}else
	{
		echo "<Br>No Data Found";
	}
	
	?>
    </table>
    <table><tr>
	<?php
	 $fav_total="select * from FAVOURITES where fav_userid=".$_SESSION['userid'];
	$fav_res=mysql_query($fav_total);
	 $fav_rows=mysql_num_rows($fav_res);
	 if($fav_rows > 5)
	 {
      $pages=ceil($fav_rows / 5);
		
		   for($i=1;$i<=$pages;$i++)
		   { 
		   ?>
				   <td class="page <?php if($i==$_REQUEST['page']){?>current<?php }?>;">
				   <a href="http://192.185.116.17/~wwwmsc/portal.php?action=profile&page=<?php echo $i;?>"><?php echo $i;?></a>
				   </td>
		   <?php  }  	
   	   }?>
    </tr></table>
    </div>
<br><br>
	<div id="favorites">
    Αγγελίες
    </div>
</div>	